package bsts.view.customer;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import bsts.model.Customer;
import bsts.model.Interest;

public class ViewCustomerPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private JPanel pnlMain;
	private JPanel pnlBasicInfo;
	private JPanel pnlCustomer;
	private JPanel pnlInterest;
	private JPanel pnlInterestDisp;

	private GridBagConstraints gbcConst;

	private JLabel lblSurname;
	private JLabel lblFirstname;
	private JLabel lblSecondname;
	private JLabel lblNric;
	private JLabel lblAddress;
	private JLabel lblTelephoneNo;
	private JLabel lblSalutation;

	private JLabel lblSurname_data;
	private JLabel lblFirstname_data;
	private JLabel lblSecondname_data;
	private JLabel lblNric_data;
	private JScrollPane scrAddress = new JScrollPane();
	private JTextArea txtAddress_data;
	private JLabel lblTelephoneNo_data;
	private JLabel lblSalutation_data;

	private JTable tblInterests;
	private JScrollPane scrInterests;

	private ArrayList<Interest> interests = new ArrayList<Interest>();

	public ViewCustomerPanel(Customer customer) {
		interests = customer.getInterests();

		pnlMain = new JPanel();
		pnlBasicInfo = new JPanel();
		pnlCustomer = new JPanel();
		pnlInterest = new JPanel();
		pnlInterestDisp = new JPanel();

		pnlMain.setLayout(new BorderLayout());
		pnlCustomer.setLayout(new GridBagLayout());
		pnlInterest.setLayout(new BoxLayout(pnlInterest, BoxLayout.Y_AXIS));
		pnlInterestDisp.setLayout(new BorderLayout());

		pnlBasicInfo.setBorder(BorderFactory.createTitledBorder("Basic Info"));
		pnlInterest.setBorder(BorderFactory.createTitledBorder("Interest"));

		gbcConst = new GridBagConstraints();

		lblSurname = new JLabel("Surname:");
		lblFirstname = new JLabel("First name:");
		lblSecondname = new JLabel("Second name:");
		lblNric = new JLabel("NRIC:");
		lblAddress = new JLabel("Address:");
		lblTelephoneNo = new JLabel("Telephone No.:");
		lblSalutation = new JLabel("Salutation:");

		lblSurname_data = new JLabel(customer.getSurname());
		lblFirstname_data = new JLabel(customer.getFirstname());
		lblSecondname_data = new JLabel(customer.getSecondname());
		lblNric_data = new JLabel(customer.getNric());
		txtAddress_data = new JTextArea(customer.getAddress(), 2, 20);
		lblTelephoneNo_data = new JLabel(customer.getTelephoneNo());
		lblSalutation_data = new JLabel(customer.getSalutation());

		scrAddress
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrAddress.add(txtAddress_data, null);
		scrAddress.setViewportView(txtAddress_data);
		txtAddress_data.setEditable(false);
		txtAddress_data.setLineWrap(true);
		txtAddress_data.setSize(150, 20);

		lblSurname_data.setPreferredSize(new Dimension(150, 20));

		tblInterests = new JTable();
		scrInterests = new JScrollPane();

		initTbl();
		scrInterests
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrInterests.add(tblInterests, null);
		scrInterests.setViewportView(tblInterests);
		scrInterests.setPreferredSize(new Dimension(500, 150));

		gbcConst.fill = GridBagConstraints.HORIZONTAL;
		gbcConst.gridx = 0;
		gbcConst.gridy = 0;
		gbcConst.gridwidth = GridBagConstraints.RELATIVE;
		pnlCustomer.add(lblSurname, gbcConst);
		gbcConst.gridx = 1;
		pnlCustomer.add(lblSurname_data, gbcConst);
		gbcConst.gridx = 0;
		gbcConst.gridy = 1;
		pnlCustomer.add(lblFirstname, gbcConst);
		gbcConst.gridx = 1;
		pnlCustomer.add(lblFirstname_data, gbcConst);
		gbcConst.gridx = 0;
		gbcConst.gridy = 2;
		pnlCustomer.add(lblSecondname, gbcConst);
		gbcConst.gridx = 1;
		pnlCustomer.add(lblSecondname_data, gbcConst);
		gbcConst.gridx = 0;
		gbcConst.gridy = 3;
		pnlCustomer.add(lblNric, gbcConst);
		gbcConst.gridx = 1;
		pnlCustomer.add(lblNric_data, gbcConst);
		gbcConst.gridx = 0;
		gbcConst.gridy = 4;
		gbcConst.ipady = 20;
		pnlCustomer.add(lblAddress, gbcConst);
		gbcConst.gridx = 1;
		pnlCustomer.add(scrAddress, gbcConst);
		gbcConst.gridx = 0;
		gbcConst.gridy = 5;
		gbcConst.ipady = 0;
		pnlCustomer.add(lblTelephoneNo, gbcConst);
		gbcConst.gridx = 1;
		pnlCustomer.add(lblTelephoneNo_data, gbcConst);
		gbcConst.gridx = 0;
		gbcConst.gridy = 6;
		pnlCustomer.add(lblSalutation, gbcConst);
		gbcConst.gridx = 1;
		pnlCustomer.add(lblSalutation_data, gbcConst);

		pnlBasicInfo.add(pnlCustomer);

		pnlInterestDisp.add(scrInterests, BorderLayout.CENTER);
		pnlInterest.add(pnlInterestDisp);

		pnlMain.add(pnlBasicInfo, BorderLayout.WEST);
		pnlMain.add(pnlInterest, BorderLayout.EAST);

		this.add(pnlMain);
	}

	public void refresh(Customer customer) {
		lblSurname_data.setText(customer.getSurname());
		lblFirstname_data.setText(customer.getFirstname());
		lblSecondname_data.setText(customer.getSecondname());
		lblNric_data.setText(customer.getNric());
		txtAddress_data.setText(customer.getAddress());
		lblTelephoneNo_data.setText(customer.getTelephoneNo());
		lblSalutation_data.setText(customer.getSalutation());
		refreshTbl();

		this.revalidate();
		this.repaint();
	}

	public void refreshTbl() {
		tblInterests.revalidate();
		tblInterests.repaint();
	}

	private void initTbl() {
		final String[] strHeaded = { "Interest", "Remark" };

		// Create a model of the data.
		TableModel dataModel = new AbstractTableModel() {
			private static final long serialVersionUID = 1L;

			public int getColumnCount() {
				return strHeaded.length;
			}

			public int getRowCount() {
				return interests.size();
			}

			public Object getValueAt(int row, int col) {
				Interest interest = interests.get(row);

				if (col == 0) {
					return interest.getName();
				} else if (col == 1) {
					return interest.getDescription();
				} else {
					return null;
				}
			}

			public String getColumnName(int column) {
				return strHeaded[column];
			}

			public Class<?> getColumnClass(int col) {
				return getValueAt(0, col).getClass();
			}
		};

		tblInterests = new JTable(dataModel);
		tblInterests.getSelectionModel();
		tblInterests.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		DefaultTableCellRenderer right = new DefaultTableCellRenderer();
		right.setHorizontalAlignment(DefaultTableCellRenderer.TRAILING);

		tblInterests.setDefaultRenderer(Object.class, right);

		int[] intWidth = { 200, 300 };

		for (int i = 0; i < intWidth.length; i++) {
			TableColumn tblCol = tblInterests.getColumn(strHeaded[i]);
			tblCol.setMaxWidth(intWidth[i]);
		}
	}

}
